﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Expense_Tracker_System
{
    public partial class MainExpenseForm : Form
    {
        #region Constructor
        public MainExpenseForm()
        {
            InitializeComponent();
        }
        #endregion

        #region Declarations
        aHelperModule helpModule = new aHelperModule();

        xExpenseForm frmExpense;
        zOptionsForm frmOptions;
        zContentsForm frmContents;
        zAboutForm frmAbout;

        private const string cmMinToTray = "1:MinToTray";
        private const string cmMaxAtStart = "2:MaxAtStart";
        private const string cmRunAtStart = "3:RunAtStart";
        #endregion

        #region Events and Methods

        private void btnAddExpense_Click(object sender, EventArgs e)
        {
            frmExpense = new xExpenseForm();
            frmExpense.myTreeView = myTreeView;
            frmExpense.myImageList = myImageList;

            frmExpense.ShowDialog();
        }

        private void btnOptions_Click(object sender, EventArgs e)
        {
            frmOptions = new zOptionsForm();

            frmOptions.ShowDialog();
        }

        private void btnContents_Click(object sender, EventArgs e)
        {
            frmContents = new zContentsForm();

            frmContents.ShowDialog();
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            frmAbout = new zAboutForm();

            frmAbout.ShowDialog();
        }

        private void MainExpenseForm_Load(object sender, EventArgs e)
        {
            try
            {
                LoadDefault();

                LoadTree();

                lblStatus.Text = "Loading Settings Success.";
                lblStatus.ForeColor = Color.Green;
            }
            catch (Exception)
            {
                lblStatus.Text = "Loading Settings Failed.";
                lblStatus.ForeColor = Color.Red;
                this.WindowState = FormWindowState.Normal;
            }
            
        }

        private void MainExpenseForm_Resize(object sender, EventArgs e)
        {
            try
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    if (helpModule.GetSettings(cmMinToTray) == true)
                    {
                        this.Hide();
                    }
                    else
                    {
                        this.WindowState = FormWindowState.Minimized;
                    }
                }
            }
            catch (Exception)
            { }
        }

        private void LoadTree()
        {
            helpModule.DisplayTreeValues(myTreeView, myImageList);
        }

        private void LoadDefault()
        {
            if (helpModule.GetSettings(cmMaxAtStart) == true)
            {
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void myTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string selValue = myTreeView.SelectedNode.Text;

            if (helpModule.CheckSelectedValue(selValue) == true)
            {
                gridViewToolStripMenuItem.Visible = false;
                graphicalViewToolStripMenuItem.Visible = true;
                myTreeView.ContextMenuStrip = myContextMenuStrip;
            }
            else if (selValue.Contains("/") == true)
            {
                gridViewToolStripMenuItem.Visible = true;
                graphicalViewToolStripMenuItem.Visible = false;
                myTreeView.ContextMenuStrip = myContextMenuStrip;
            }
            else
            {
                gridViewToolStripMenuItem.Visible = false;
                graphicalViewToolStripMenuItem.Visible = false;
            }
        }

        private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myTabControl.TabPages.Remove(myTabControl.SelectedTab);
        }

        private void myNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.Activate();
            this.BringToFront();
            LoadDefault();
        }

        private void gridViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime selectedDate;

            if (myTreeView.SelectedNode.Text != "Expense Dates")
            {
                selectedDate = DateTime.Now;
                selectedDate = Convert.ToDateTime(myTreeView.SelectedNode.Text);
                helpModule.DisplayDataGridTabValues(selectedDate, myTabControl);
            }
        }

        private void graphicalViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string[] selectedMonth;
            string selectedYear;

            if (myTreeView.SelectedNode.Text != "Expense Dates")
            {
                selectedMonth = null;
                selectedMonth = myTreeView.SelectedNode.Text.Split(':');
                selectedYear = myTreeView.SelectedNode.PrevVisibleNode.Text;

                helpModule.DisplayGraphicalTabValues(selectedMonth[0].ToString(), selectedYear, myTabControl);
            }
        }
        
        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadTree();
        }

        //private void refreshTabToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    string selValue = myTabControl.SelectedTab.Text;

        //    if (selValue.Contains("Grid View") == true)
        //    {
        //        selValue = selValue.Replace("Grid View: ", "");

        //        gridViewToolStripMenuItem_Click(sender, e);
        //    }
        //    else if (selValue.Contains("Graph View") == true)
        //    {
        //        selValue = selValue.Replace("Graph View: ", "");

        //        graphicalViewToolStripMenuItem_Click(sender, e);
        //    }
        //}
        #endregion
    }
}
